package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.User;
import cc.yugu.yhdw.common.domain.hdw.BigContent;
import cc.yugu.yhdw.common.domain.hdw.CabinetStrategy;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.entity.SessionUser;
import cc.yugu.yhdw.webserver.service.hdw.BigContentService;
import cc.yugu.yhdw.webserver.service.hdw.CabinetStrategyService;
import cc.yugu.yhdw.webserver.service.hdw.AgencyService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping(value = "/security/hdw/cabinet_strategy")
public class CabinetStrategyController extends SecurityController {
    @Autowired
    CabinetStrategyService cabinetStrategyService;
    @Autowired
    BigContentService bigContentService;
    @Autowired
    AgencyService agencyService;

    @ResponseBody
    @ViewModel(ViewModel.JSON_ARRAY)
    @RequestMapping(value = "tree.htm")
    public void tree(Integer agentId, Integer agencyId, HttpServletResponse response) throws Exception {
        response.setContentType(ConstEnum.ContentType.JSON.getValue());
        cabinetStrategyService.tree(agentId,agencyId, response.getOutputStream());
    }
    @RequestMapping(value = "index.htm")
    public void index(Model model) throws Exception {
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_02_02.getValue());
    }

    @RequestMapping("page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(CabinetStrategy search,HttpServletRequest request) {
        if (getSessionUser(request).getType() == User.Type.AGENT.getValue()) {
            search.setAgentId(getSessionUser(request).getAgentId());
        } else if (getSessionUser(request).getType() == User.Type.AGENCY.getValue()) {
            search.setAgencyId(getSessionUser(request).getAgencyId());
        }
        return PageResult.successResult(cabinetStrategyService.findPage(search));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public void add() {
    }

    @ResponseBody
    @RequestMapping(value = "create.htm")
    public ExtResult create(CabinetStrategy entity, String content) {
        return cabinetStrategyService.create(entity, content);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("edit.htm")
    public String edit(int id, Model model) {
        CabinetStrategy entity = cabinetStrategyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }

        model.addAttribute("entity", entity);
        return "/security/hdw/cabinet_strategy/edit";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping("load.htm")
    public ExtResult load(int id) {
        String content = bigContentService.find(BigContent.Type.CABINET_STRATEGY.getValue(), id);
        if (StringUtils.isNotEmpty(content)) {
            return DataResult.successResult((Object) content);
        } else {
            return ExtResult.failResult("记录不存在");
        }
    }

    @ResponseBody
    @RequestMapping(value = "update.htm")
    public ExtResult update(CabinetStrategy entity, String content) {
        return cabinetStrategyService.update(entity, content);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("view.htm")
    public String view(int id, Model model) {
        CabinetStrategy entity = cabinetStrategyService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        }
        model.addAttribute("entity", entity);
        return "/security/hdw/cabinet_strategy/view";
    }

    @ResponseBody
    @RequestMapping(value = "delete.htm")
    public ExtResult delete(int id) {
        return cabinetStrategyService.delete(id);
    }

}
